<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/24 0024
 * Time: 上午 10:42
 */
namespace app\admin\controller;
use think\Db;
use think\Loader;

class Saletop extends Datasta {
    /*
     * 商品销量top10
     * */
    public function salelist(){
        $datas=Db::name("goods")->order('sale_num desc')->limit(10)->field('goods_name,sale_num')->select();
        foreach ($datas as $k=>$v){
            $data['num'][$k]=$v['sale_num'];
            $data['name'][$k]=$v['goods_name'];
        }
        $this->assign('num',json_encode($data['num']));
        $this->assign('name',json_encode($data['name']));
        return $this->fetch();
    }
    /*
    * 店铺销量top10
    * */
    public function shoplist(){
        $datas=Db::name("shop")->where('shop_status',1)->order('sale_num desc')->limit(10)->field('shop_name,sale_num')->select();
        foreach ($datas as $k=>$v){
            $data['num'][$k]=$v['sale_num'];
            $data['name'][$k]=$v['shop_name'];
        }
        $this->assign('num',json_encode($data['num']));
        $this->assign('name',json_encode($data['name']));
        return $this->fetch();

    }
    /*
     * 骑手评论统计
     * */
    public function  ridersta(){
        if($this->request->isAjax()){
            $riderid=$this->request->param('r_id'); //获取骑手id
            $start=input("start");
            $end=input("end");
            $datas=$this->getdatas($riderid,strtotime($start),strtotime($end));
            return json($datas);
        }else{
            //查询所有骑手
            $rider=Db::name('rider')->order('id asc')->select();
            $datas=$this->getdatas($rider[0]['id'],strtotime('-7 days'),time());
            $this->assign('man',json_encode($datas['man'],true));
            $this->assign('num',json_encode($datas['num'],true));
            $this->assign('remarknum',json_encode($datas['remarknum'],true));
            $this->assign('spe',json_encode($datas['spe'],true));
            $this->assign('timelists',json_encode($datas['timelists'],true));
            $this->assign('rider',$rider);
            return $this->fetch();
        }


    }

    public function getdatas($riderid,$start,$end){
        $data= $this->settimess($start,$end);
        $timelists=settime($data);//x轴时间
        $num=[];
        foreach ($timelists as $k=>$v){
            //订单最后完成时间
            $wheres['o_lasttime'] =['between',[strtotime($v.' '.'00:00:00'),strtotime($v.' '.'23:59:59')]];
            $rider_id=$riderid;
            //查询订单数（时间段内某个骑手的订单数）
            $num[$k]=Db::name('orderinfo')->where($wheres)->where('r_id',$rider_id)->count();
            //查询订单时间段内已评论订单数
            $remarknum[$k]=Db::name('riderremark')
                ->alias('a')
                ->join('orderinfo b','a.r_order=b.id')
                ->where($wheres)
                ->where('r_riderid',$rider_id)
                ->count();
            //查询时间段内订单号
            $orderid[$k]=Db::name('orderinfo')->where($wheres)->where('r_id',$rider_id)->field('id')->select();
            foreach($orderid[$k] as $kk=>$vv){
                $orderid[$k]['ids'][$kk]=$vv['id'];
            }
            if(is_array($orderid[$k]['ids'])) {
                $man[$k]=Db::name('riderremark')->where('r_order', in, $orderid[$k]['ids'])->avg('r_manner');
                $spe[$k]=Db::name('riderremark')->where('r_order', in, $orderid[$k]['ids'])->avg('r_speed');
                if($man[$k]==null){
                    $man[$k]=0;
                }
                if($spe[$k]==null){
                    $spe[$k]=0;
                }
            }else{
                $man[$k]=0;
                $spe[$k]=0;
            }
        }
        $datas['man']=$man;//服务星值
        $datas['spe']=$spe;//物流星值
        $datas['num']=$num;//订单数
        $datas['remarknum']=$remarknum;//评论数
        $datas['timelists']=$timelists;//时间
        $datas['end']=$data['end'];//时间
        $datas['begin']=$data['begin'];//时间
        $this->assign('end',date('Y-m-d',$data['end']));
        $this->assign('begin',date('Y-m-d',$data['begin']));
        return $datas;
    }
}